package com.sonicboom.nurun.dao;

import org.skife.jdbi.v2.sqlobject.Bind;
import org.skife.jdbi.v2.sqlobject.GetGeneratedKeys;
import org.skife.jdbi.v2.sqlobject.SqlQuery;
import org.skife.jdbi.v2.sqlobject.SqlUpdate;
import org.skife.jdbi.v2.sqlobject.mixins.GetHandle;

public abstract class IncentiveDAO implements GetHandle {

    @SqlUpdate("insert into user (name, email) values (:name, :email)")
    @GetGeneratedKeys
    public abstract long addUser(@Bind("name") String userName, @Bind("email") String email);

    public long tryAddUser(String userName) {
        long userId = getUser(userName);
        if (userId > 0) {
            return userId;
        } else {
            return addUser(userName, "");
        }
    }


    @SqlQuery("select id from user where name = :name")
    public abstract long getUser(@Bind("name") String userName);

    public void addLuckyDrawRecord(long userId, LifeStyle lifeStyle, String token) {
        addLuckyDrawRecord(userId, lifeStyle.getCode(), lifeStyle.getName(), true, token);
    }

    @SqlQuery("select count(*) from luckydrawrecord l inner join address a on l.userid = a.userid where l.token = :token")
    public abstract int addressAlreadyBeLogged(@Bind("token") String token);

    public boolean isValidToken(String token) {
        return addressAlreadyBeLogged(token) == 0;
    }

    @SqlQuery("select token from luckydrawrecord where userid = :userid")
    public abstract String getToken(@Bind("userid") long userId);

    @SqlUpdate("insert into luckydrawrecord (userid, lifestyleid, lifestyle, win, token)" +
            " values (:userid, :lifestyleid, :lifestyle, :win, :token)")
    public abstract void addLuckyDrawRecord(@Bind("userid") long userId,
                                            @Bind("lifestyleid") int lifeStyleId,
                                            @Bind("lifestyle") String lifeStyle,
                                            @Bind("win") boolean win,
                                            @Bind("token") String token);


    @SqlUpdate("insert into address (userid, realname, province, city, detailaddress, contact)" +
            " values (:userid, :realname, :province, :city, :detailaddress, :contact)")
    public abstract void addUserAddress(@Bind("userid") long userId,
                                        @Bind("realname") String realName,
                                        @Bind("province") String province,
                                        @Bind("city") String city,
                                        @Bind("detailaddress") String detailAddress,
                                        @Bind("contact") String contact);


    @SqlUpdate("insert into prizerule (unfreezetime, quotation) values(:unfreezetime, :quotation)")
    public abstract void addPrizeRule(@Bind("unfreezetime") String unfreezeTime, @Bind("quotation") int quotation);

    @SqlQuery("select count(*) from luckydrawrecord where win = true")
    public abstract int getAlreadyGivenPrizeCount();

    @SqlQuery("select count(*) = 1 from luckydrawrecord l " +
            " left outer join address a on a.userid = l.userid" +
            " where l.userid = :userid and l.win = true and a.userid is null")
    public abstract boolean isALuckyUserButAddressNotFill(@Bind("userid") long userId);


    @SqlQuery("select sum(quotation) from prizerule where unfreezetime <= CURRENT_TIMESTAMP")
    public abstract int getAvailablePrizeCount();
}
